package com.xuln.ipp.dao.impl;

import java.util.List;

import com.xuln.common.util.PaginationSupport;
import com.xuln.framework.dao.BaseDao;
import com.xuln.framework.entity.PrimaryKey;
import com.xuln.ipp.dao.IArticleDao;
import com.xuln.ipp.entity.Article;

public class ArticleDaoImpl extends BaseDao<Article, PrimaryKey> implements
		IArticleDao {

	@SuppressWarnings("unchecked")
	public List<Article> getPublicArticle(int... resultSize) {
		PaginationSupport page;
		String hql = "from Article where isPublication=true";
		if (resultSize.length == 0) {
			page = findPageByQuery(hql, 0);
		} else {
			page = findPageByQuery(hql, 0, resultSize[0]);
		}
		return page.getItems();
	}

	@SuppressWarnings("unchecked")
	public List<Article> getArticleByCatetoryId(long catetoryId,
			int... resultSize) {
		PaginationSupport page;
		String hql = "from Article where isPublication=true and category.id=?";
		if (resultSize.length == 0) {
			page = findPageByQuery(hql, 0, catetoryId);
		} else {
			page = findPageByQuery(hql, 0, resultSize[0], catetoryId);
		}
		return page.getItems();
	}

	public void deleteById(Long articleId) {
		String hql = "from Article a where a.PK.id=?";
		List<Article> articles = find(hql, articleId);
		deleteAll(articles);
	}

	public List<Article> getAllArticle(String locale) {
		String hql = "from Article a where a.PK.locale=?";
		return find(hql, locale);
	}

	public Article getArticle(long id, String locale) {
		return get(new PrimaryKey(id, locale));
	}

	public List<Article> getArticleByCatetoryId(long catetoryId, String locale) {
		String hql = "from Article a where  a.categoryId=? and a.PK.locale=?";
		return find(hql, catetoryId, locale);
	}
}
